package com.lanxiao.cloud.modules.oauth.domain.model;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.google.common.collect.Lists;
import com.lanxiao.cloud.common.core.enums.DelFlagEnum;
import com.lanxiao.cloud.common.core.enums.MethodEnum;
import com.lanxiao.cloud.common.core.enums.StateEnum;
import com.lanxiao.cloud.common.core.utils.StringUtils;
import com.lanxiao.cloud.common.mybatis.basic.BaseEntity;
import com.lanxiao.cloud.modules.oauth.infrastructure.enums.ButtonEventEnum;
import com.lanxiao.cloud.modules.oauth.infrastructure.enums.PositionEnum;
import com.lanxiao.cloud.modules.oauth.infrastructure.enums.TypeEnum;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.springframework.util.AntPathMatcher;

/**
 * @author ：qiaoliang
 * @date ：2021-08-24
 */
@TableName(value = "sys_permission",autoResultMap = true)
public class Permission extends BaseEntity<Long, Long> {
  /**父级ID**/
  private Long parentId;
  /**父级ID集合**/
  private String parentIds;
  /**名称**/
  private String title;
  /**标识**/
  private String mark;
  /**类型**/
  private TypeEnum type;
  /**请求url**/
  private String url;
  /**请求类型**/
  private MethodEnum method;
  /**重定向地址**/
  private String redirect;
  /**按钮位置**/
  private PositionEnum position;
  /**事件类型**/
  private ButtonEventEnum eventType;
  /**事件**/
  private String event;
  /**排序**/
  private int sort;
  /**状态**/
  private StateEnum state;
  /**图标**/
  private String icon;
  /**路由地址**/
  private String path;
  /**路由路径**/
  private String component;
  /**路由名称**/
  private String name;

  /**
   *  校验是否需要权限访问
   *
   * @author ：qiaoliang
   * @param url : 请求地址
   * @param method : 请求方式
   * @return boolean
   * @date 2021-09-07 15:52
   */
  public boolean isPermission(String url, MethodEnum method){
    if (StringUtils.isBlank(this.getUrl())) {
      return false;
    }
    return new AntPathMatcher().match(this.getUrl(), url)
        && this.getMethod().equals(method);
  }

  /**
   *  反向修改状态
   *
   * @author ：qiaoliang
   * @date 2020-10-20 10:29
   */
  public void updateState(){
    this.setState(
        this.getState().equals(StateEnum.NORMAL)
        ?StateEnum.ERROR
        :StateEnum.NORMAL);
  }

  /**
   *  填充父级ID
   *
   * @author ：qiaoliang
   * @param parent : 父级权限
   * @date 2021-10-22 11:51
   */
  public void fillParentIds(Permission parent) {
    if (Objects.nonNull(parent)) {
      String parentIds = parent.getParentIds();
      final List<Long> list = JSONUtil.toList(JSONUtil.parseArray(parentIds), Long.class);
      list.add(parent.getId());
      this.setParentIds(JSONUtil.toJsonStr(list));
    }
  }

  public Long getParentId() {
    return parentId;
  }

  public String getParentIds() {
    return parentIds;
  }

  public void setParentIds(String parentIds) {
    this.parentIds = parentIds;
  }

  public void setParentId(Long parentId) {
    this.parentId = parentId;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getMark() {
    return mark;
  }

  public void setMark(String mark) {
    this.mark = mark;
  }

  public TypeEnum getType() {
    return type;
  }

  public void setType(TypeEnum type) {
    this.type = type;
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public String getRedirect() {
    return redirect;
  }

  public void setRedirect(String redirect) {
    this.redirect = redirect;
  }

  public MethodEnum getMethod() {
    return method;
  }

  public void setMethod(MethodEnum method) {
    this.method = method;
  }

  public int getSort() {
    return sort;
  }

  public void setSort(int sort) {
    this.sort = sort;
  }

  public PositionEnum getPosition() {
    return position;
  }

  public void setPosition(PositionEnum position) {
    this.position = position;
  }

  public ButtonEventEnum getEventType() {
    return eventType;
  }

  public void setEventType(ButtonEventEnum eventType) {
    this.eventType = eventType;
  }

  public String getEvent() {
    return event;
  }

  public void setEvent(String event) {
    this.event = event;
  }

  public StateEnum getState() {
    return state;
  }

  public void setState(StateEnum state) {
    this.state = state;
  }

  public String getIcon() {
    return icon;
  }

  public void setIcon(String icon) {
    this.icon = icon;
  }

  public String getPath() {
    return path;
  }

  public void setPath(String path) {
    this.path = path;
  }

  public String getComponent() {
    return component;
  }

  public void setComponent(String component) {
    this.component = component;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}
